package com.ruoyi.system.outto.goods.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.model.domain.*;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.core.OutUrlConstant;
import com.ruoyi.system.outto.core.RegionalCovert;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.game.ITQYIAPI;
import com.ruoyi.system.outto.goods.NEWITQYIAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/21 13:30
 */
@Slf4j
@Service
public class NEWTQYYAPIImpl implements NEWITQYIAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Autowired
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;
    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Resource
    private ArtRecMapper artRecMapper;

    public static void main(String[] args) {
//        String APPID =  "9964";
//        String TOKEN = "c629a1a0a891379024ac1a4e971baec1";
//
        Map<String,Object> params = new HashMap<>();
//        params.put("appId",APPID);
//        params.put("token",TOKEN);
//        params.put("title","1211312");
//        params.put("introduce","11123");
//        params.put("content","1123");
//        params.put("bprice","1213");
//        params.put("price","1231");
//        params.put("gameId","205");
//        params.put("areaId","213");
//        params.put("roles","213");
//        params.put("isBface","0");
//        params.put("isBtel","0");
//        params.put("isMail","0");
//        params.put("productType","1");
//        params.put("grade","10");
//        params.put("pic1","https://game.ikbh.top/logo/no_img.jpg");
//        params.put("param1","20");
//        params.put("param2","20");
//        params.put("isIndemnity","1");
//        params.put("isRecovery","1");
//        params.put("changeGameRealName","2");
//
//
//        params.put("parentid","2");
//        String cover = MapUntils.getCover(params);
//        cover = cover.substring(0,cover.length()-1);
//        String baseUrl = "http://b.taoqiyou.com";
//////        String path = "/api/open/v1/product/get-area?parentid=2";
//        String path = "/api/open/v1/product/get-area?"+cover;
//        String response = HttpUtils.sendPost(baseUrl + path, null, "application/x-www-form-urlencoded");
        String response = getQufu("1975");
        System.out.println(response);

//        long time = DateUtils.getNowDate().getTime();
//        System.out.println(time);
//        String s = "特斯拉X紫色";
//        String s1 = s.replace("特斯拉","特斯拉Model").replace("3","3(").replace("X","X(").replace("色",")");
//        System.out.println(s1);

        /**
         * 获取游戏ID
         *
         */
    }

    static String getQufu(String id){
        Map<String,Object> params = new HashMap<>();
        params.put("parentid",id);
        String cover = MapUntils.getCover(params);
        cover = cover.substring(0,cover.length()-1);
        String baseUrl = "http://b.taoqiyou.com";
        String path = "/api/open/v1/product/get-area?"+cover;
        String response = HttpUtils.sendPost(baseUrl + path, null, "application/x-www-form-urlencoded");
        return response;
    }
    static String getRole(String id){
        Map<String,Object> params = new HashMap<>();
        params.put("gameId",id);
        String cover = MapUntils.getCover(params);
        cover = cover.substring(0,cover.length()-1);
        String baseUrl = "http://b.taoqiyou.com";
        String path = "/api/open/v1/product/get-role?"+cover;
        String response = HttpUtils.sendPost(baseUrl + path, null, "application/x-www-form-urlencoded");
        return response;
    }




    private Map<String,Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner sa){
        GameAttribute gameAttribute = null;
        if(goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF16()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("和平精英")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("原神")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("穿越火线")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("光遇")){//=================================未配置游戏属性
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("火影忍者")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF7()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("逆战")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        }else{
            throw new GlobalException("当前游戏暂不支持");
        }


        Map<String,Object> params = new HashMap<>();
        params.put("token",this.salePartner.getOther());
        params.put("appId",this.salePartner.getSaleKey());
        String title = goodsInfoAndAttrValueDto.getDes();
        if (goodsInfoAndAttrValueDto.getDes().length() > 150){
            title = goodsInfoAndAttrValueDto.getDes().substring(0,150);
        }
        //标题
        params.put("title","【"+descTitle+"_"+goodsInfoAndAttrValueDto.getId() + "】" + title);
        params.put("introduce","【"+descTitle+"_"+goodsInfoAndAttrValueDto.getId() + "】" + goodsInfoAndAttrValueDto.getDes());
        String content = "<h3>" + goodsInfoAndAttrValueDto.getDes() + "</h3>";
        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        if (CollectionUtils.isEmpty(infoImgs)){
            params.put("pic1",this.salePartner.getTwoOther());
        }else {
            if (infoImgs.size() > 5){
                for (int i=1;i <=5; i++){
                    params.put("pic"+i,infoImgs.get(i-1).getImgUrl());
                }
            }else {
                for (int i=1;i <=infoImgs.size(); i++){
                    params.put("pic"+1,infoImgs.get(i-1).getImgUrl());
                }
            }
        }
        StringBuffer imgs = new StringBuffer();
        if (!CollectionUtils.isEmpty(infoImgs)){
            for (GoodsInfoImg img : infoImgs) {
                String imgStr = img.getImgUrl()+" ";
                imgs.append("<img src="+imgStr+"/>");
            }
        }
        params.put("content",content+imgs.toString());
        BigDecimal bigDecimal = commonApi.addPrice(goodsInfoAndAttrValueDto.getPrice(), sa.getSeleep());
        commonApi.setPrice(bigDecimal,goodsInfoAndAttrValueDto.getId(),sa);
        params.put("bprice",bigDecimal.stripTrailingZeros().toPlainString());
        params.put("price",bigDecimal.stripTrailingZeros().toPlainString());
        String type = RegionalCovert.typeToTQY(goodsInfoAndAttrValueDto.getGameName());
        if (StringUtils.isNull(type)){
            return AjaxResult.error("游戏类型未找到");
        }
        params.put("gameId",type);
        String roles = "";
        if(goodsInfoAndAttrValueDto.getGameName().contains("火影忍者")){
            if (Objects.equals("安卓微信", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1967");
            } else if (Objects.equals("安卓QQ", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1929");
            } else if (Objects.equals("苹果微信", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1968");
            } else if (Objects.equals("苹果QQ", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1930");
            }
            JSONArray jsonArray1 = JSONArray.parseArray(JSONObject.parseObject(getRole("209")).getString("data"));
            for(Object o : jsonArray1){
                if(JSONObject.parseObject(o.toString()).getString("n").equals("S级忍者")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF5())) {
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF5());
                        String s = JSONObject.parseObject(o.toString()).getString("s");
                        JSONArray jsonArray = JSONArray.parseArray(s);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray){
                                if(gameAttribute1.getFieldName().equals(JSONObject.parseObject(p.toString()).getString("n"))){
                                    roles += "," + JSONObject.parseObject(p.toString()).getString("v");
                                }
                            }
                        }
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("n").equals("A级忍者")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF6())) {
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF6());
                        String s = JSONObject.parseObject(o.toString()).getString("s");
                        JSONArray jsonArray = JSONArray.parseArray(s);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray){
                                if(gameAttribute1.getFieldName().equals(JSONObject.parseObject(p.toString()).getString("n"))){
                                    roles += "," + JSONObject.parseObject(p.toString()).getString("v");
                                }
                            }
                        }
                    }
                }
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")){
            JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(getQufu("34")).getString("data"));
            for(Object o : jsonArray){
                if(goodsInfoAndAttrValueDto.getServerName().contains(JSONObject.parseObject(o.toString()).getString("name"))){
                    params.put("areaId","34");
                    params.put("serverId",JSONObject.parseObject(o.toString()).getString("id"));
                    break;
                }
            }
            JSONArray jsonArray1 = JSONArray.parseArray(JSONObject.parseObject(getQufu("35")).getString("data"));
            for(Object o : jsonArray1){
                if(goodsInfoAndAttrValueDto.getServerName().contains(JSONObject.parseObject(o.toString()).getString("name"))){
                    params.put("areaId","35");
                    params.put("serverId",JSONObject.parseObject(o.toString()).getString("id"));
                    break;
                }
            }
            JSONArray jsonArray2 = JSONArray.parseArray(JSONObject.parseObject(getQufu("37")).getString("data"));
            for(Object o : jsonArray2){
                if(goodsInfoAndAttrValueDto.getServerName().contains(JSONObject.parseObject(o.toString()).getString("name"))){
                    params.put("areaId","37");
                    params.put("serverId",JSONObject.parseObject(o.toString()).getString("id"));
                    break;
                }
            }
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF3())){
                params.put("param1",0);
            }else {
                params.put("param1",goodsInfoAndAttrValueDto.getF3());
            }
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF4())){
                params.put("param2",0);
            }else {
                params.put("param2",goodsInfoAndAttrValueDto.getF4());
            }

            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF7());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF8());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF9());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF10());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF12());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF13());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF14());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF15());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF16());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF17());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            JSONArray jsonArray3 = JSONArray.parseArray(JSONObject.parseObject(getRole("2")).getString("data"));
            for(GameAttribute gameAttribute1 : gameAttributesss){
                if(gameAttribute1.getFieldName().contains("龙年盲僧")){
                    gameAttribute1.setFieldName("龙虾");
                }
                if(gameAttribute1.getFieldName().contains("拉莫斯国王")){
                    gameAttribute1.setFieldName("国王龙龟");
                }
                if(gameAttribute1.getFieldName().contains("诸神的黄昏")){
                    gameAttribute1.setFieldName("诸神黄昏");
                }
                if(gameAttribute1.getFieldName().contains("海克斯龙龟")){
                    gameAttribute1.setFieldName("海克斯拉莫斯");
                }
                if(gameAttribute1.getFieldName().contains("海克斯牛头")){
                    gameAttribute1.setFieldName("海克斯阿利斯塔");
                }
                if(gameAttribute1.getFieldName().contains("海克斯大嘴")){
                    gameAttribute1.setFieldName("海克斯克格莫");
                }
                if(gameAttribute1.getFieldName().contains("海克斯希维尔")){
                    gameAttribute1.setFieldName("新创战纪希维尔");
                }
                if(gameAttribute1.getFieldName().contains("海克斯皇子")){
                    gameAttribute1.setFieldName("海克斯嘉文四世");
                }
                if(gameAttribute1.getFieldName().contains("摄魂薇恩")){
                    gameAttribute1.setFieldName("摄魂使者薇恩");
                }
                if(gameAttribute1.getFieldName().contains("海克斯梦魇")){
                    gameAttribute1.setFieldName("海克斯魔腾");
                }
                if(gameAttribute1.getFieldName().contains("至臻 腥红之月 剑魔")){
                    gameAttribute1.setFieldName("至臻亚托克斯");
                }
                for(Object o : jsonArray3){
                    JSONArray jsonArray4 = JSONArray.parseArray(JSONObject.parseObject(o.toString()).getString("s"));
                    for(Object p:jsonArray4) {
                        if (gameAttribute1.getFieldName().replace(" ","").replace("K/DA","").replace("真实伤害","")
                                .replace("暗星","").replace("奥术师","").replace("魔女","")
                                .replace("未来战士","").replace("灵魂莲华","").replace("神龙尊者","")
                                .replace("星之守护者","").replace("王国机神玄武","").replace("玉剑传说","")
                                .replace("电玩女神","").replace("源计划：正义","").replace("战斗学院","").replace("烈焰美人","")
                                .replace("绝息圣堂","").contains(JSONObject.parseObject(p.toString())
                                .getString("n").replace("科技","")) ||JSONObject.parseObject(p.toString()).getString("n")
                                .replace("科技","").contains(gameAttribute1.getFieldName().replace(" ","").replace("K/DA","").replace("真实伤害","")
                                        .replace("暗星","").replace("奥术师","").replace("魔女","")
                                        .replace("未来战士","").replace("灵魂莲华","").replace("神龙尊者","")
                                        .replace("星之守护者","").replace("王国机神玄武","").replace("玉剑传说","")
                                        .replace("电玩女神","").replace("源计划：正义","").replace("战斗学院","").replace("烈焰美人","")
                                        .replace("绝息圣堂",""))) {
                            roles += "," + JSONObject.parseObject(p.toString()).getString("v");
                        }
                    }
                }
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().contains("和平精英")){
            if (Objects.equals("安卓微信", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1917");
            } else if (Objects.equals("安卓QQ", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1950");
            } else if (Objects.equals("苹果微信", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1918");
            } else if (Objects.equals("苹果QQ", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1951");
            }
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF8())){
                params.put("param1",0);
            }else {
                params.put("param1",goodsInfoAndAttrValueDto.getF8());
            }
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF9())){
                params.put("param2",0);
            }else {
                params.put("param2",goodsInfoAndAttrValueDto.getF9());
            }
            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF1());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF2());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF3());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF4());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF5());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(getRole("207")).getString("data"));
            for(GameAttribute gameAttribute1 : gameAttributesss){
                String fieldName = gameAttribute1.getFieldName();
                if(fieldName.contains("路特斯EV紫色")){
                    fieldName = "路特斯EVIJA（电光紫）";
                }else if(fieldName.contains("兰博轿车灰色")){
                    fieldName = "兰博基尼Est（灰）";
                }else if(fieldName.contains("兰博轿车金色")){
                    fieldName = "兰博基尼Est（金）";
                }else if(fieldName.contains("兰博轿车尊享铂")){
                    fieldName = "兰博基尼Est（铂）";
                }else if(fieldName.contains("兰博SUV黄色")){
                    fieldName = "兰博基尼URUS(明艳黄）";
                }else if(fieldName.contains("兰博SVJ绿色")){
                    fieldName = "兰博基尼SVJ（绿）";
                }else if(fieldName.contains("兰博SVJ尊享铂")){
                    fieldName = "兰博基尼SVJ（铂）";
                }else if(fieldName.contains("兰博SUV尊享铂")){
                    fieldName = "兰博基尼URUS(尊享铂）";
                }else if(fieldName.contains("阿斯顿马丁")){
                    fieldName = fieldName.substring(fieldName.length()-2)+fieldName.replace("阿斯顿马丁","阿斯顿马丁DB").substring(0,fieldName.length());
                }else if(fieldName.contains("特斯拉3")||fieldName.contains("特斯拉X")){
                    fieldName = fieldName.replace("特斯拉","特斯拉Model").replace("3","3(").replace("X","X(").replace("色",")");
                }else if(fieldName.contains("玛莎拉蒂金色")){
                    fieldName = "金色玛莎拉蒂";
                }else if(fieldName.contains("玛莎拉蒂粉色")){
                    fieldName = "粉色玛莎拉蒂";
                }else if(fieldName.contains("玛莎拉蒂蓝色")){
                    fieldName = "蓝色玛莎拉蒂";
                }else if(fieldName.contains("兰博SUV尊享铂")){
                    fieldName = "兰博基尼SUV（铂）";
                }else if(fieldName.contains("兰博SUV黄色")){
                    fieldName = "兰博基尼SUV（黄）";
                }else if(fieldName.contains("兰博SUV金色")){
                    fieldName = "兰博基尼SUV（金）";
                }
                for(Object o : jsonArray){
                    JSONArray jsonArray1 = JSONArray.parseArray(JSONObject.parseObject(o.toString()).getString("s"));
                    for(Object p : jsonArray1){
                        String name = JSONObject.parseObject(p.toString()).getString("n").replace("-","");
                        if(name.contains("合金龙骨")||name.contains("双刃魅影")){
                            name = name.replace("（","").replace("）","")+"色";
                        }else if(name.contains("特斯拉Roa-")){
                            name = name.replace("oa-","");
                        }else if(name.contains("特斯拉Cyb-")){
                            name = name.replace("yb-","");
                        }else if(name.contains("空客H135")){
                            name = name.replace("H135","");
                        }else if(name.contains("塞莎KL27-")){
                            name = name.replace("KL27-","");
                        }
                        if(fieldName.contains(name)||name.contains(fieldName)){
                            roles+=","+JSONObject.parseObject(p.toString()).getString("v");
                        }
                    }
                }
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")){
            if (Objects.equals("安卓微信", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1965");
            } else if (Objects.equals("安卓QQ", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1923");
            } else if (Objects.equals("苹果微信", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1966");
            } else if (Objects.equals("苹果QQ", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","1924");
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")){
            if (Objects.equals("安卓微信", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","213");
            } else if (Objects.equals("安卓QQ", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","212");
            } else if (Objects.equals("苹果微信", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","211");
            } else if (Objects.equals("苹果QQ", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName())) {
                params.put("areaId","210");
            }
            if(goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")){
                if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF7())){
                    params.put("param1",0);
                }else {
                    params.put("param1",goodsInfoAndAttrValueDto.getF7());
                }
                if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF8())){
                    params.put("param2",0);
                }else {
                    params.put("param2",goodsInfoAndAttrValueDto.getF8());
                }
                String pfIds = "";
                pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF9());
                pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF10());
                pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
                pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF12());
                pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF15());
                List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
                JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(getRole("205")).getString("data"));
                for(GameAttribute gameAttribute1 : gameAttributesss) {
                    for(Object o : jsonArray){
                        JSONArray jsonArray1 = JSONArray.parseArray(JSONObject.parseObject(o.toString()).getString("s"));
                        for(Object p : jsonArray1){
                            if(gameAttribute1.getFieldName().contains(JSONObject.parseObject(p.toString()).getString("n"))){
                                roles+=","+JSONObject.parseObject(p.toString()).getString("v");
                            }
                        }
                    }
                }
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().contains("穿越火线")){
            if(goodsInfoAndAttrValueDto.getGroupName().equals("东部战区")){
                params.put("areaId","2000");
            }else if(goodsInfoAndAttrValueDto.getGroupName().equals("西部战区")){
                params.put("areaId","2001");
            }else if(goodsInfoAndAttrValueDto.getGroupName().equals("南部战区")){
                params.put("areaId","2002");
            }else{
                params.put("areaId","1835");
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().contains("原神")){
            if(goodsInfoAndAttrValueDto.getCarrierName().contains("哔哩哔哩")){
                params.put("areaId","1998");
            }else if(goodsInfoAndAttrValueDto.getCarrierName().contains("游戏官方")){
                params.put("areaId","1971");
            }else {
                params.put("areaId","1972");
            }
            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF17());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(getRole("1970")).getString("data"));
            for(GameAttribute gameAttribute1:gameAttributesss){
                for(Object o : jsonArray){
                    JSONArray jsonArray1 = JSONArray.parseArray(JSONObject.parseObject(o.toString()).getString("s"));
                    for(Object p : jsonArray1){
                        if(gameAttribute1.getFieldName().contains(JSONObject.parseObject(p.toString()).getString("n"))){
                            roles+=","+JSONObject.parseObject(p.toString()).getString("v");
                        }
                    }
                }
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().contains("逆战")){
            if(goodsInfoAndAttrValueDto.getGroupName().contains("电信")){
                params.put("areaId","1986");
            }else{
                params.put("areaId","1987");
            }
        }
        if(StringUtils.isNotEmpty(roles)) {
            params.put("roles", roles.substring(1));
        }
        if(goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")||goodsInfoAndAttrValueDto.getGameName().contains("和平精英")){
            if(gameAttribute.getFieldName().contains("包人脸")||gameAttribute.getFieldName().contains("可二次实名")){
                params.put("isBface","1");
                params.put("changeGameRealName","1");
            }else{
                params.put("isBface","0");
                params.put("changeGameRealName","0");
            }
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("逆战")){
            if(gameAttribute.getFieldName().contains("包人脸")||gameAttribute.getFieldName().contains("可实名")){
                params.put("isBface","1");
                params.put("changeGameRealName","1");
            }else{
                params.put("isBface","0");
                params.put("changeGameRealName","0");
            }
        } else if(goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")){
            params.put("isBface","1");
            params.put("changeGameRealName","1");
        } else if(goodsInfoAndAttrValueDto.getGameName().contains("原神")){
            if(gameAttribute.getFieldName().contains("有修改人脸机会")||gameAttribute.getFieldName().contains("已删人脸实名可随时认证")){
                params.put("isBface","1");
                params.put("changeGameRealName","1");
            }else{
                params.put("isBface","0");
                params.put("changeGameRealName","0");
            }
        }
        params.put("grade","10");
        params.put("otherNo",goodsInfoAndAttrValueDto.getId());

        params.put("isBtel","0");
        params.put("isMail","0");
        params.put("productType","1");
        params.put("isIndemnity","1");
        params.put("isRecovery","1");
        return params;

    }

    private List<GameAttribute> getGameAttributs(String ids) {
        return gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                Long.class));
    }
    private String reIds(String allIds, String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            if (StringUtils.isNotEmpty(allIds)) {
                allIds = allIds + "," + ids;
            } else {
                allIds = ids;
            }
        }
        return allIds;
    }

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(goodsInfoAndAttrValueDto,salePartner);
        String path = "/api/open/v1/product/add-product";
        String paramsStr = JSONObject.toJSONString(params);
        log.info("淘气游新增请求参数={}",paramsStr);
        String response = HttpUtils.doPostForm(OutUrlConstant.TQYURL + path,params,null);
        log.info("淘气游新增返回参数={}",response);
        JSONObject resultJson = JSONObject.parseObject(response);
        String msg = resultJson.getString("msg");
        msg = MapUntils.unicodeToString(msg);
        if (!"0".equals(resultJson.getString("code"))){
//            return AjaxResult.error(msg);
            throw new GlobalException(msg);
        }

        Map<String, Object> result = new HashMap<>();
        if (!"0".equals(resultJson.getString("code"))){
            result.put("code",500);
        }else {
            result.put("code",200);
            String data = resultJson.getString("data");
            ArtRec artRecreq = new ArtRec();
            artRecreq.setConfig(salePartner.getId());
            artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
            List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
            if(artRecs.size()>0) {
                artRecs.get(0).setOutid(data);
                artRecMapper.updateArtRec(artRecs.get(0));
            }else{
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                artRecreq.setCreateId(goodsInfoAndAttrValueDto.getCreateId());
                artRecreq.setType(LCConstant.interAdd);
                artRecreq.setConfig(salePartner.getId());
                artRecreq.setSaleCode(salePartner.getSaleName());
                artRecreq.setOutid(data);
                artRecMapper.insertArtRec(artRecreq);
            }
        }

        result.put("req",JSONObject.toJSONString(params));
        result.put("res",response);
        result.put("msg",msg);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(goodsInfoAndAttrValueDto,salePartner);
        String path = "/api/open/v1/product/update-product";
        String paramsStr = JSONObject.toJSONString(params);
        log.info("淘气游编辑请求参数={}",paramsStr);
        String response = HttpUtils.doPostForm(OutUrlConstant.TQYURL + path,params,null);
        log.info("淘气游编辑返回参数={}",response);
        JSONObject resultJson = JSONObject.parseObject(response);
        String msg = resultJson.getString("msg");
        msg = MapUntils.unicodeToString(msg);
        Map<String, Object> result = new HashMap<>();
        if (!"0".equals(resultJson.getString("code"))){
            result.put("code",500);
        }else {
            result.put("code",200);
        }
        result.put("req",JSONObject.toJSONString(params));
        result.put("res",response);
        result.put("msg",msg);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto,SalePartner sa) {
        Map<String,Object> params = new HashMap<>();
        params.put("appId",sa.getSaleKey());
        params.put("token",sa.getOther());
        params.put("otherNo",goodsInfoAndAttrValueDto.getId());
        String paramsStr = JSONObject.toJSONString(params);
        log.info("淘气游删除请求参数={}",paramsStr);
        String path = "/api/open/v1/product/product-lower";
        String response = HttpUtils.doPostForm(OutUrlConstant.TQYURL + path,params,null);
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isNotEmpty(response)){
            log.info("淘气游删除返回参数={}",response);
            JSONObject resultJson = JSONObject.parseObject(response);
            String msg = resultJson.getString("msg");
            msg = MapUntils.unicodeToString(msg);
            if (!"0".equals(resultJson.getString("code"))){
                result.put("code",500);
            }else {
                result.put("code",200);
                ArtRec artRecreq = new ArtRec();
                artRecreq.setConfig(salePartner.getId());
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
                if(artRecs.size()>0) {
                    artRecMapper.deleteArtRecById(artRecs.get(0).getId());
                }
            }
            result.put("req",JSONObject.toJSONString(params));
            result.put("res",response);
            result.put("msg",msg);
        }else {
            result.put("code",500);
            result.put("req",JSONObject.toJSONString(params));
            result.put("res","操作失败");
            result.put("msg","操作失败");
        }
        return AjaxResult.success(result);
    }

}
